var data = ""
var deptId = ""


var InterfacelistInfoDlg = {
    data: {
        id: "",
        name: "",
        loc: "",
        paramId: "",
        returnSample: "",
        revision: "",
        createdBy: "",
        createdTime: "",
        updatedBy: "",
        updatedTime: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects', 'xmSelect', 'func', 'layer'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var xmSelect = layui.xmSelect;
    var func = layui.func;
    var layer = layui.layer;

    data = JSON.parse(sessionStorage.getItem('data'))
    console.log(data)
    // sessionStorage.clear()

    /**
     * 查看详情前获取当前用户权限
     */
    function getUser() {
        $.ajax(Feng.ctxPath + '/databaseListKh/getUser', {
            success: function (res) {
                // console.log(res)
                deptId = res.data.deptId;
            }
        })
    }


    /**
     * 给参数下拉列表赋值
     */
    var xSelect;
    $.ajax(Feng.ctxPath + "/interfaceparam/list", {
        success: function (obj) {
            strArr = obj.data;
            var newArr = [];
            for (var i = 0; i < strArr.length; i++) {
                newArr.push({name: strArr[i].param + '：' + strArr[i].detail, value: strArr[i].id,});
            }
            xSelect = xmSelect.render({
                el: '#param',
                toolbar: {
                    show: true,
                    showIcon: true,
                },
                filterable: true,
                layVerify: 'required',
                tips: '如果没有想要的参数,点击左侧参数添加',
                paging: true,
                pageSize: 5,
                // remoteSearch: true,
                // pageRemote: true,
                data: newArr
            })
        }
    })

    /**
     * 点击提交按钮
     */
    $("#btnSubmit").click(function () {
        var name = $("#name").val()
        var loc = $("#loc").val()
        var description = $('#description').val()
        var returnSample = $("#returnSample").val()
        var paramId = ''
        var param = xSelect.getValue();
        for (var i = 0; i < param.length; i++) {
            if (param.length == 1) {
                paramId = param[i].value.toString()
            } else {
                if (i < param.length - 1) {
                    paramId = paramId + param[i].value + ','
                }
                if (i == param.length - 1) {
                    paramId = paramId + param[i].value
                }
            }
        }
        $.ajax(Feng.ctxPath + "/interfacelist/addItem", {
            data: {
                name: name,
                loc: loc,
                returnSample: returnSample,
                paramId: paramId,
                description: description,
                interfaceapprovalId: data.id
            },
        })
        if (data != null && data != '' && data != undefined) {
            $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
                data: {
                    id: data.id,
                    userId: data.userId,
                    interfaceId: data.interfaceId,
                    content: data.content,
                    serviceid: data.serviceid,
                    department: data.department,
                    echo: 0,
                    description: data.description,
                    system: data.system,
                    servicetype: data.servicetype,
                    idescribe: description,
                    state: 3,
                    remarks: data.remarks,
                },
                // success: function () {
                //     sessionStorage.clear()
                // }
            })
            $.ajax(Feng.ctxPath + '/dbNotice/complete',{
                data:{
                    id:data.noticeId
                }
            })
        }
        // else {
        //     if (data != null || data != '') {
        //         $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
        //             data: {
        //                 id: data.id,
        //                 userId: data.userId,
        //                 interfaceId: data.interfaceId,
        //                 content: data.content,
        //                 serviceid: data.serviceid,
        //                 department: data.department,
        //                 echo: 0,
        //                 description: data.description,
        //                 system: data.system,
        //                 servicetype: data.servicetype,
        //                 idescribe: description,
        //                 state: 3,
        //                 remarks: data.remarks,
        //             },
        //             success: function () {
        //                 sessionStorage.clear()
        //             }
        //         })
        //     }
        // }
        admin.closeThisDialog()
        sessionStorage.clear()
    })

    /**
     * 点击取消返回接口列表
     */
    $("#returnParent").click(function () {
        // history.go(-1)
        sessionStorage.clear()
        layer.closeAll()
    })


    /**
     * 点击参数添加参数
     */

    $("#addParam").click(function () {
        sessionStorage.setItem("listAdd", "listAdd")
        layer.open({
            title: '',
            area: ['100%', '100%'],
            content: Feng.ctxPath + "/interfaceparam/add",
            cancel: function () {
                window.location.href = Feng.ctxPath + '/interfacelist/add'
            },
            type: 2,
            maxmin: true,
            success: function (layero) {
                layero.find('.layui-layer-min').remove();
            },
            end: function () {
                // admin.closeThisDialog()
                location.reload()
            },
        })

    })


    /**
     * 文本域高度自适应
     */
    //jQuery实现textarea高度根据内容自适应
    $.fn.extend({
        txtaAutoHeight: function () {
            return this.each(function () {
                var $this = $(this);
                if (!$this.attr('initAttrH')) {
                }
                setAutoHeight(this).on('input', function () {
                    setAutoHeight(this);
                });
            });

            function setAutoHeight(elem) {
                var $obj = $(elem);
                return $obj.css({height: $obj.attr('initAttrH'), 'overflow-y': 'hidden'}).height(elem.scrollHeight);
            }
        }
    });
    //调用
    $(function () {
        $("#returnSample").txtaAutoHeight();
        getUser();
    });

    // console.log(JSON.parse(sessionStorage.getItem('data')))
    // console.log(sessionStorage.getItem('addInterface'))

});